#!/bin/python
# -*- coding: utf-8 -*-
"""
Name: path_util.py
Author: harrybao
Mail: harrybao@123u.com
Time: 2021/07/12 16:54
Desc: 路径工具
"""

import os
import platform
import shutil

class PathUtil():
    """路径工具类"""
    def __init__(self):
        self._guild_service_root_path = os.path.join(
            os.path.abspath(os.path.dirname(__file__)), '..')

        self._guild_service_package_path = os.path.join(
            self._guild_service_root_path, 'package')
        
        self._guild_service_tools_path = os.path.join(
            self._guild_service_root_path, 'tools')

        self._service_box_root_path = os.path.join(
            self._guild_service_root_path, '../../../..')
        
        self._service_box_publish_path = os.path.join(
            self._service_box_root_path, 'publish/bin/service')
        
        self._service_box_repo_path = os.path.join(
            self._service_box_root_path, 'src/repo')

    @property
    def guild_service_root_path(self):
        """公会服务根目录"""
        return self._guild_service_root_path
    
    @property
    def guild_service_package_path(self):
        """公会服务包目录"""
        return self._guild_service_package_path
    
    @property
    def guild_service_tools_path(self):
        """公会服务工具目录"""
        return self._guild_service_tools_path

    @property
    def service_box_root_path(self):
        """ServiceBox根目录"""
        return self._service_box_root_path

    @property
    def service_box_publish_path(self):
        """ServiceBox发布目录"""
        return self._service_box_publish_path
    
    @property
    def service_box_repo_path(self):
        """ServiceBox仓库目录"""
        return self._service_box_repo_path

    def check_path(self, path, print_error=True):
        """检查路径"""
        if not os.path.exists(path):
            if print_error:
                print('path not exists: {0}'.format(path))
            return False
        else:
            return True

    def go_to_service_box_dir(self):
        """跳转到ServiceBox根目录"""
        if os.getcwd() != self._service_box_root_path:
            os.chdir(self._service_box_root_path)

    def go_to_guild_service_dir(self):
        """跳转到GuildService根目录"""
        if os.getcwd() != self._guild_service_root_path:
            os.chdir(self._guild_service_root_path)
    
    def go_to_service_box_repo_dir(self):
        """跳转到GuildService仓库目录"""
        if os.getcwd() != self._service_box_repo_path:
            os.chdir(self._service_box_repo_path)
    
    def remove_path(self, path, print_error=True):
        """删除目录"""
        if not os.path.exists(path):
            if print_error:
                print('path not exist: {0}'.format(path))
            return False

        shutil.rmtree(path)
        return True